<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
	<meta charset="UTF-8">
    <title>Web sockets test</title>
    
    <script type="text/javascript">
        var ws;
        function ToggleConnectionClicked(token) {          
        	 var uid = 12345678
                try {
					ws = new WebSocket("ws://test-txcc.ctest.baijiahulian.com/ws/tx-normal?user_token=" + token );
					
					ws.onopen = function(event){
                    	alert("已经与服务器建立了连接\r\n当前连接状态："+this.readyState);
                    	//setInterval(keeplive,100000);
                    	};
					ws.onmessage = function(event){
						alert("接收到服务器发送的数据：\r\n"+event.data);
						};
					ws.onclose = function(event){
						//alert("接收到服务器发送的数据：\r\n"+event.data);
						alert("已经与服务器断开连接\r\n当前连接状态："+this.readyState);
						};
					ws.onerror = function(event){
						alert("WebSocket异常！"+event);
					};
                } catch (ex) {
                    alert(ex.message);      
				}
        };
 
        function SendData() {
			try{
				var sendName = '{ "type": "biz","date":"beston"}';
				ws.send(sendName);
				
			}catch(ex){
				alert(ex.message);
			}
        };
 
		function seestate(){
			alert(ws.readyState);
		}
       
		function keeplive(){
			try{
				var ping = '{ "type": "ping"}';
				ws.send(ping);
				
			}catch(ex){
				alert(ex.message);
			}
		}
        
		function sendMessage(){
			var message = document.getElementById('messageInput').value.trim();
			
			try{
				ws.send(message);
			}catch(ex){
				alert(ex.message);
			}
		}
        
		
    </script>
</head>
<body>
   <textarea id="tokenInput" rows="4" style="width:400px;"></textarea>
   <button id='ToggleConnection' type="button">连接服务器</button>
   <form action="">
   </form>
   <br /><br />
    <button id='sendMyName' type="button" onclick='SendData();'>发送我的名字：beston</button><br /><br />
    <button id='checkStatus' type="button" onclick='seestate();'>查看状态</button><br /><br />
    <button id='sendKeepLive' type="button" onclick='keeplive();'>发送心跳</button><br /><br />
    
    <textarea id="messageInput" rows="4" style="width:400px;"></textarea>
    <button id='messageSend' type="button" onclick='sendMessage();'>发送消息</button><br /><br />
</body>

<script>
    document.getElementById('ToggleConnection').addEventListener('click', function () {
        ToggleConnectionClicked(
            document.getElementById('tokenInput').value.trim()
        )
    });
</script>
</html>